<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影院名称" prop="cinemaName">
        <el-input
          v-model="queryParams.cinemaName"
          placeholder="请输入电影院名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cinema:cinema:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cinema:cinema:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cinema:cinema:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cinema:cinema:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cinemaList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="电影院id" align="center" prop="cinemaId" />-->
      <el-table-column label="影院名称" align="center" prop="cinemaName" />
      <el-table-column label="电话" align="center" prop="cinemaPhone" />
      <el-table-column label="省" align="center" prop="cinemaProvince" :formatter="convertCode"/>
      <el-table-column label="城市" align="center" prop="cinemaCity" :formatter="convertCode"/>
      <el-table-column label="区域" align="center" prop="cinemaArea" :formatter="convertCode"/>
      <el-table-column label="详细地址" align="center" prop="cinemaLocation" />
      <el-table-column label="品牌" align="center" prop="cinemaBrand" :formatter="convertBrand"/>
      <el-table-column label="影院照片" align="center" prop="cinemaImg" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.cinemaImg"></el-image>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cinema:cinema:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cinema:cinema:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电影院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="品牌" prop="cinemaBrand">
<!--          <el-input v-model="form.cinemaBrand" placeholder="请输入品牌" />-->
          <el-select v-model="form.cinemaBrand" placeholder="请选择">
            <el-option
              v-for="item in brandList"
              :key="item.brandId"
              :label="item.brandName"
              :value="item.brandId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="影院名称" prop="cinemaName">
          <el-input v-model="form.cinemaName" placeholder="请输入电影院名称" />
        </el-form-item>
        <el-form-item label="电话" prop="cinemaPhone">
          <el-input v-model="form.cinemaPhone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="省" prop="cinemaProvince">
          <el-select v-model="form.cinemaProvince" placeholder="请选择" @change="changeProvince">
            <el-option
              v-for="item in provinceList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaProvince" placeholder="请输入影院所在的省" />-->
        </el-form-item>
        <el-form-item label="城市" prop="cinemaCity">
          <el-select v-model="form.cinemaCity" placeholder="请选择" @change="changeCity">
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaCity" placeholder="请输入影院所在城市" />-->
        </el-form-item>


        <el-form-item label="区域" prop="cinemaArea">
            <el-select v-model="form.cinemaArea" placeholder="请选择">
            <el-option
              v-for="item in countyList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaArea" placeholder="请输入区域" />-->
        </el-form-item>

        <el-form-item label="详细地址" prop="cinemaLocation">
          <el-input v-model="form.cinemaLocation" placeholder="请输入详细地址" />
        </el-form-item>

        <el-form-item label="影院照片" prop="cinemaImg">
          <el-upload
            action="http://upload.wncinema.com/file/upload"
            list-type="picture-card"
            :headers="head"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
<!--          <el-input v-model="form.cinemaImg" placeholder="请输入影院照片" />-->
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCinema, getCinema, delCinema, addCinema, updateCinema, exportCinema } from "@/api/cinema/cinema";
import areas from '@/utils/areas'
import { listBrand} from "@/api/cinema/brand";
import { getToken } from '@/utils/auth'
export default {
  name: "Cinema",
  components: {
  },
  data() {
    return {
      head:{},
      dialogImageUrl: '',
      dialogVisible: false,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //省列表
      provinceList:[],
      countyList:[],
      cityList:[],
      // 电影院信息表格数据
      cinemaList: [],

      brandList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cinemaName: null,
        cinemaPhone: null,
        cinemaArea: null,
        cinemaLocation: null,
        cinemaBrand: null,
        cinemaImg: null,
        cinemaCity: null,
        cinemaLng: null,
        cinemaLat: null,
        cinemaProvince: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getProvinceList();
    this.getBrandList();
    this.getList();
    this.head={'Authorization':'Bearer ' + getToken()}

  },
  methods: {
    handleSuccess(response, file, fileList){
      console.log('file url',response)
      this.form.cinemaImg=response;

    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //选择省
    changeProvince(){

      // 清空城市和区县列表
      this.cityList=[];
      this.countyList=[];
      this.form.cinemaCity=undefined;
      this.form.cinemaArea=undefined;

      //form.cinemaProvince
      for(let i=0;i<areas.length;i++){
        if(areas[i].type==2 && areas[i].parentId==this.form.cinemaProvince){
          this.cityList.push(areas[i]);
        }
      }

    },
    changeCity(){
      this.countyList=[];
      this.form.cinemaArea=undefined;
      for(let i=0;i<areas.length;i++){
        if(areas[i].type==3 && areas[i].parentId==this.form.cinemaCity){
          this.countyList.push(areas[i]);
        }
      }
    },
    getProvinceList(){
      for(let i=0;i<areas.length;i++){
        if(areas[i].type==1){
          this.provinceList.push(areas[i]);
        }
      }
    },
//获取品牌列表
    getBrandList(){
      listBrand({
              pageNum: 1,
              pageSize: 1000}
      ).then(response => {
        this.brandList = response.rows;
      });
    },
    convertBrand(row, column, cellValue, index){
      for(let i=0;i<this.brandList.length;i++){
        if(this.brandList[i].brandId==cellValue){
          return this.brandList[i].brandName;
        }
      }
    },
    convertCode(row, column, cellValue, index){
      for(let i=0;i<areas.length;i++){
        if(areas[i].id==cellValue){
          return areas[i].name;
        }
      }
    },
    /** 查询电影院信息列表 */
    getList() {
      this.loading = true;
      listCinema(this.queryParams).then(response => {
        this.cinemaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cinemaId: null,
        cinemaName: null,
        cinemaPhone: null,
        cinemaArea: null,
        cinemaLocation: null,
        cinemaBrand: null,
        cinemaImg: null,
        cinemaCity: null,
        cinemaLng: null,
        cinemaLat: null,
        cinemaProvince: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cinemaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电影院信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cinemaId = row.cinemaId || this.ids
      getCinema(cinemaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电影院信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cinemaId != null) {
            updateCinema(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCinema(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cinemaIds = row.cinemaId || this.ids;
      this.$confirm('是否确认删除电影院信息编号为"' + cinemaIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCinema(cinemaIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有电影院信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCinema(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
